package com.xiao.community.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.xiao.community.entity.PintuanEntity;
import com.xiao.community.service.PintuanService;
import com.xiao.community.utils.OSSFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiao.community.entity.GoodsEntity;
import com.xiao.community.service.GoodsService;
import com.xiao.group.common.utils.PageUtils;
import com.xiao.group.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 
 *
 * @author 韩国涛
 * @email 2845009006@gmail.com
 * @date 2021-09-10 16:12:39
 */
@RestController
@RequestMapping("community/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private PintuanService pintuanService;

    @RequestMapping("togoodsList")
    public R togoodslistsss(){
        return goodsService.togoodslists();
    }
    @RequestMapping("togoodslist")
    public  List<GoodsEntity> togoodsList(@RequestParam("idss")String idss){
        return goodsService.togoodsList(idss);
    }
    @RequestMapping("togoodslists")
    public  List<GoodsEntity> togoodslists(){
        return goodsService.list();
    }

    @RequestMapping("toes")
    public R toes(@RequestParam(value = "mohu",defaultValue = "")String mohu,
                  @RequestParam(value = "page",defaultValue = "1")Integer page,
                  @RequestParam(value = "size",defaultValue = "3")Integer size,
                  @RequestParam(value = "goodsId",defaultValue ="" )Integer goodsId) throws IOException {
        return goodsService.toes(mohu,page,size,goodsId);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/tolist")
    public R tolist(){
        return goodsService.tolist();
    }
    @RequestMapping("/tolists")
    public R tolists(){
        return goodsService.tolists();
    }

    /**
     * 上传图片
     */
    @RequestMapping("toimg")
    public R toimg(MultipartFile file) throws IOException {
        String s = OSSFileUtil.uploadFile(file);
        return R.ok().put("data",s);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{goodsId}")
    public R info(@PathVariable("goodsId") Integer goodsId) {
        GoodsEntity goods = goodsService.getById(goodsId);

        return R.ok().put("goods", goods);
    }
    @RequestMapping("/toinfo")
    public GoodsEntity toinfo(@RequestParam("goodsId") Integer goodsId) {
        return   goodsService.getById(goodsId);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GoodsEntity goods){
		goodsService.save(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] goodsIds){
		goodsService.removeByIds(Arrays.asList(goodsIds));

        return R.ok();
    }
    @Scheduled(cron = "0/50 * * * * ?")
    public void  tocron(){
        List<PintuanEntity> list = pintuanService.list();
        for (PintuanEntity pintuanEntity : list) {
            Date date = new Date();
            if(date.getTime() > pintuanEntity.getStartTime().getTime() &&date.getTime()<pintuanEntity.getEndTime().getTime()){
                GoodsEntity goodsEntity = goodsService.getById(pintuanEntity.getGooGoodsId());
                goodsEntity.setGoodsStatus(1);
                pintuanEntity.setPintuanStatus(1);
                pintuanService.updateById(pintuanEntity);
                goodsService.updateById(goodsEntity);
            }
            if(date.getTime() > pintuanEntity.getEndTime().getTime()){
                GoodsEntity goodsEntity = goodsService.getById(pintuanEntity.getGooGoodsId());
                goodsEntity.setGoodsStatus(0);
                pintuanEntity.setPintuanStatus(0);
                pintuanService.updateById(pintuanEntity);
                goodsService.updateById(goodsEntity);
            }
            if(date.getTime() < pintuanEntity.getStartTime().getTime() ){
                GoodsEntity goodsEntity = goodsService.getById(pintuanEntity.getGooGoodsId());
                goodsEntity.setGoodsStatus(2);
                goodsService.updateById(goodsEntity);
            }
        }
    }
}
